package com.woniu.humanresource.controller;

import com.woniu.humanresource.entity.Lecturer;
import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.LecturerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
public class LecturerController {
    @Resource
    LecturerService lecturerService;

    // 删除讲师
    @DeleteMapping("/lecturer/{lid}")
    public ResponseText deleteLecturer(@PathVariable("lid") int lid){
        ResponseText responseText = null;
        try {
            lecturerService.deleteLecturer(lid);
            responseText = new ResponseText(null,200,"success");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    // 添加讲师
    @PostMapping("/lecturer")
    public ResponseText addLecturer(@RequestBody Lecturer lecturer){
        ResponseText responseText = null;
        try {
        lecturerService.insertLecturer(lecturer);
        responseText = new ResponseText(null,200,"success");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    // 修改讲师
    @PutMapping("/lecturer")
    public ResponseText updateLecturer(@RequestBody Lecturer lecturer){
        ResponseText responseText = null;
        try {
            lecturerService.updateLecturer(lecturer);
            responseText = new ResponseText(null,200,"success");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    // 查询讲师
    @PostMapping("/lecturer/{pageIndex}/{pageSize}")
    public ResponseText selectLecturer(@PathVariable("pageIndex") int pageIndex,
                                       @PathVariable("pageSize") int pageSize,
                                       @RequestBody Lecturer lecturer){
        ResponseText<PageBean<Lecturer>> responseText = null;
        try {
            PageBean<Lecturer> pageBean = lecturerService.selectLecturerCondition(lecturer, pageIndex, pageSize);
            responseText = new ResponseText<PageBean<Lecturer>>(pageBean,200,"success");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    // 获取全部讲师
    @GetMapping("/lecturer")
    public ResponseText getAllLecturer(){
        ResponseText responseText = null;
        try {
            List<Lecturer> allLecturer = lecturerService.getAllLecturer();
            responseText = new ResponseText(allLecturer,200,"获取成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
